package com.nercel.dsj.gksales.api.controller;

import com.nercel.dsj.gksales.model.QueryParams;
import com.nercel.dsj.gksales.model.dto.RegionBasicResultDTO;
import com.nercel.dsj.gksales.model.entity.Region;
import com.nercel.dsj.gksales.service.RegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author sunlin
 * @date 2018-10-26
 */
@RestController
@Api(tags = "地区接口")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class RegionApiController extends BaseApiController {

    private final RegionService regionService;

    @GetMapping(value = "/api/v1/regions")
    @ApiOperation(value = "地区查询")
    public List<RegionBasicResultDTO> query(@ApiParam(hidden = true) QueryParams queryParams,
            @ApiParam(value = "关键词") @RequestParam(required = false) String keyword,
            @ApiParam(value = "同级ID") @RequestParam(required = false) Integer siblingId,
            @ApiParam(value = "父级ID") @RequestParam(required = false) Integer parentId
    ) {
        queryParams.put("keyword", keyword);
        queryParams.put("parentId", parentId);
        queryParams.put("siblingId", siblingId);
        return regionService.queryWithSimpleInfo(queryParams);
    }

    @GetMapping(value = "/api/v1/regions/{id}")
    @ApiOperation(value = "地区查询")
    public Region show(
                @ApiParam(value = "关键词") @PathVariable Integer id
    ) {
        return regionService.findById(id);
    }

}
